/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
// 快速排序中的一次分割，将数组分为三部分
var sortColors = function(nums) {
    let left = 0, right = nums.length
    let i = 0
    while(i < right) {
        if(nums[i] == 0) {
            swap(nums, i, left)
            i++
            left++
        } else if(nums[i] == 1) {
            i++
        } else {
            right--
            swap(nums, i, right)
        }
    }
};
var swap = function(nums, i, j) {
    let temp = nums[i]
    nums[i] = nums[j]
    nums[j] = temp
}
// 还可以对 0 1 2的个数进行计数，三次计数
var sortColors = function(nums) {
    let count = 0 // 用于计数，已经放置好的元素的个数
    for(let i = 0; i < 3; i++) {
        for(let j = count; j < nums.length; j++) {
            if(nums[j] === i) {
                temp = nums[j]
                nums[j] = nums[count]
                nums[count] = temp
                count++
            }
        }
    }
}